package com.changfeng.service.impl;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.RetailersUser;
import com.changfeng.bean.dto.vo.UserAccountUpdateVO;
import com.changfeng.bean.dto.vo.UserEmailModifyVO;
import com.changfeng.bean.dto.vo.UserHeadModifyVO;
import com.changfeng.bean.dto.vo.UserPasswordModifyVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.dao.RetailersUserMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RetailersUserMapper retailersUserMapper;

    @Override
    public RetailersUser loginUser(String userName, String password) {

        return retailersUserMapper.login(userName,password);
    }

    @Override
    public RetailersUser getUserByKey(Integer userId) {
        return retailersUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public Integer signUser(RetailersUser retailersUser) {
        RetailersUser check = retailersUserMapper.checkUserName(retailersUser.getUserName());
        if (check != null) {
            throw new ChangFengException(ChangFengExceptionEnum.USER_NAME_REPEAT);
        }
        retailersUser.setPowerId(0);
        retailersUser.setGmtCreate(new Date());
        retailersUser.setGmtModified(new Date());
        return retailersUserMapper.insert(retailersUser);
    }

    @Override
    public RetailersUser updateUserByAccount(UserAccountUpdateVO accountUpdateVO) {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        retailersUser.setUserNickname(accountUpdateVO.getNickName());
        retailersUser.setUserBirthday(accountUpdateVO.getDate());
        retailersUser.setUserPhone(accountUpdateVO.getPhone());
        retailersUser.setUserSex(accountUpdateVO.getSex());
        retailersUserMapper.updateByPrimaryKey(retailersUser);
        return null;
    }

    @Override
    public Integer userPasswordModify(UserPasswordModifyVO userPasswordModifyVO) {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        if (!retailersUser.getUserPassword().equals(userPasswordModifyVO.getUsedPassword())) {
            throw new ChangFengException(ChangFengExceptionEnum.USER_CHECK_PASSWORD_ERROR);
        }
        if (userPasswordModifyVO.getUsedPassword().equals(userPasswordModifyVO.getPassword())) {
            throw new ChangFengException(ChangFengExceptionEnum.USER_CHECK_PASSWORD_ERROR).setMessage("修改密码与原密码相同");
        }
        retailersUser.setUserPassword(userPasswordModifyVO.getPassword());
        retailersUserMapper.updateByPrimaryKey(retailersUser);

        return null;
    }

    @Override
    public Integer userEmailModify(UserEmailModifyVO userEmailModifyVO) {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }

        if(userEmailModifyVO.getEmail().equals(retailersUser.getUserEmail())) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION).setMessage("邮箱与原邮箱相同");
        }

        retailersUser.setUserEmail(userEmailModifyVO.getEmail());
        retailersUserMapper.updateByPrimaryKey(retailersUser);
        return null;
    }

    @Override
    public Integer userHeadModify(UserHeadModifyVO userHeadModifyVO) {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }

        retailersUser.setUserHeadurl(userHeadModifyVO.getImageUrl());
        retailersUserMapper.updateByPrimaryKey(retailersUser);
        return null;
    }
}
